
    import { 
        getCompanyPageList,
        getCompanyDetail,
        addCompany,
        delCompany,
        modifyCompany,
        changeCompanyState,
        getCompanyTreeSelectList
    } from '@/api/baseFrame/base/company.js';
    import Treeselect from '@riophae/vue-treeselect'
    import '@riophae/vue-treeselect/dist/vue-treeselect.css'
    import { getCompanyTreeSelectList as companyTreeSelectDataQuery } from '@/api/baseFrame/base/company'

    export default {
        name: "company",
        components: { Treeselect },
        data() {
            return {
                labelWidth: "100px",
                formLabelWidth:"100px",
                // 选中companyId数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 遮罩层
                loading: false,
                // 显示搜索条件
                showSearch: true,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    sort: undefined,
                    sortType: undefined,
                    setTime: undefined,
                    stateCode: undefined,
                },
                companyTreeFormOptions: undefined,           
                treeFilterKeywords:undefined,
                companyTreeOptions:undefined,
                defaultTreeProps: {
                    children: 'children',
                    label: 'label',
                },
                noOptionsText: '暂无数据',
                noChildrenText: '暂无子项',
                noResultsText: '未找到匹配项',
                loadingText: '数据加载中...',
                // 弹出层标题
                title: "",
                companyTreeSelectFormOptions:undefined,
                // 操作类型 1、add 2、edit
                opertype: 0,
                // 是否显示弹出层
                open: false,
                // 表单参数
                form: {},
                columns: [
                    { index: 0, key: 'companyId', label: `公司标识`, checked:  true  },
                    { index: 1, key: 'paCompanyId', label: `上级公司`, checked:  true  },
                    { index: 2, key: 'paCompanyName', label: `上级`, checked:  true  },
                    { index: 3, key: 'companyName', label: `公司名`, checked:  true  },
                    { index: 4, key: 'companyAbbr', label: `公司简称`, checked:  true  },
                    { index: 5, key: 'companyNo', label: `公司编号`, checked:  true  },
                    { index: 6, key: 'setTime', label: `成立时间`, checked:  true  },
                    { index: 7, key: 'createTime', label: `创建时间`, checked:  true  },
                    { index: 8, key: 'creater', label: `创建人`, checked:  true  },
                    { index: 9, key: 'stateCode', label: `状态`, checked:  false  },
                ],
                //成立时间时间范围
                dateRangeSetTime: [],
                // 状态选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
                data_state: [],
                dataList: [],
                total: 0,
                rules: {
                    companyId: [{ required: true, message: "公司标识不能为空", trigger: "blur" }],
                    paCompanyId: [{ required: true, message: "上级公司不能为空", trigger: "blur" }],
                    companyName: [{ required: true, message: "公司名不能为空", trigger: "blur" }],
                    companyAbbr: [{ required: true, message: "公司简称不能为空", trigger: "blur" }],
                    companyNo: [{ required: true, message: "公司编号不能为空", trigger: "blur" }],
                    stateCode: [{ required: true, message: "状态不能为空", trigger: "blur" }],
                },
            };
        },
        watch: {
            // 根据名称筛选树结点
            treeFilterKeywords(val) {
                this.$refs.tree.filter(val)
            },
        },
        created() {
            this.getCompanyTreeSelectList()
            this.companyFormTreeOptionsQuery()
            // 列表数据查询
            this.getList();
            var dictParams = [
                "data_state",
            ];
            this.getDicts(dictParams).then((response) => {
                response.data.forEach((element) => {
                    this[element.dictType] = element.list;
                });
            });
            this.companyTreeSelectDataQuery({'IsReturnRoot':false,'OnlyLevel1Node':false});
        },
        methods: {
            normalizer(node) {
                return {
                    id: node.id,
                    label: node.label,
                    children: node.children,
                    isDisabled: node.isDisabled==true || node.isDisabled=='True'
                };
            },
            companyFormTreeOptionsQuery(){
                getCompanyTreeSelectList({RootNodeName:'根结点',OnlyLevel1Node:false}).then((response) => {
                    this.companyTreeFormOptions = response.data
                })
            },
            getCompanyTreeSelectList() {
                getCompanyTreeSelectList({OnlyLevel1Node:false}).then((response) => {
                    this.companyTreeOptions = response.data
                })
            },
            // 节点单击事件
            handleNodeClick(data) {
                this.queryParams.treeNodeId = data.id
                this.queryParams.isDefaultNodeType=data.isDefaultNodeType
                this.getList()
            },
            // 筛选节点
            filterNode(value, data) {
                if (!value) return true
                return data.label.indexOf(value) !== -1
            },
            // 查询数据
            getList() {
                this.addDateRange(this.queryParams, this.dateRangeSetTime, 'SetTime');
                this.loading = true;
                getCompanyPageList(this.queryParams).then(res => {
                    if (res.code == 200) {
                        this.dataList = res.data.result;
                        this.total = res.data.totalNum;
                        this.loading = false;
                    }
                })
            },
            companyTreeSelectDataQuery(data) {
                companyTreeSelectDataQuery(data).then((response) =>
                {
                    this.companyTreeSelectFormOptions = response.data
                })
            },
            handlecompanyTreeSelectSelected(node)
            {
            },

            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 重置数据表单
            reset() {
                this.form = {
                    companyId: undefined,
                    paCompanyId: undefined,
                    companyName: undefined,
                    companyAbbr: undefined,
                    companyNo: undefined,
                    belongIndustry: undefined,
                    manager: undefined,
                    tel: undefined,
                    email: undefined,
                    fax: undefined,
                    addr: undefined,
                    zip: undefined,
                    companyUrl: undefined,
                    setTime: undefined,
                    runscope: undefined,
                    remark: undefined,
                    stateCode: 'Enabled',
                };
                this.resetForm("form");
            },
            // 重置查询操作
            resetQuery() {
                this.timeRange = [];
                //成立时间时间范围
                this.dateRangeSetTime= [];
                this.resetForm("queryForm");
                this.queryParams.treeNodeId=undefined;
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                    this.ids = selection.map((item) => item.companyId);
                    this.single = selection.length != 1
                    this.multiple = !selection.length;
            },
            // 自定义排序
            sortChange(column) {
                    if (column.prop == null || column.order == null) {
                        this.queryParams.sort = undefined;
                        this.queryParams.sortType = undefined;
                    } else {
                        this.queryParams.sort = column.prop;
                        this.queryParams.sortType = column.order;
                    }

                    this.handleQuery();
            },
            handleStatusChange(row) {
                let text = row.stateCode == 'Enabled' ? '启用' : '禁用'
                this.$confirm('确认要' + text + '吗?', '警告', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                })
                .then(function () {
                    return changeCompanyState({ "id": row.companyId, "stateCode": row.stateCode })
                })
                .then(() => {
                    this.msgSuccess(text + '成功')
                })
                .catch(function () {
                    row.stateCode = row.stateCode == 'Disabled' ? 'Enabled' : 'Disabled'
                })
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加";
                this.opertype = 1;
                this.form.paCompanyId=this.queryParams.treeNodeId||0;
            },

            /** 删除按钮操作 */
            handleDelete(row) {
                    const Ids = row.companyId || this.ids;
                    var confirmTip='确认要删除数据项吗？';
                    if(row.dataName)
                    {
                            confirmTip='确认要删除数据项吗？';
                    }
                    this.$confirm(confirmTip)
                    .then(function () {
                        return delCompany({"Ids":Ids+''});
                    })
                    .then(() => {
                        this.handleQuery();
                        this.getCompanyTreeSelectList();
                        this.msgSuccess("删除成功");
                    })
                    .catch(() => {});
            },

            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.companyId || this.ids;
                getCompanyDetail(id).then((res) => {
                        const { code, data } = res;
                        if (code == 200) {
                                this.open = true;
                                this.title = "修改数据";
                                this.opertype = 2;
                                this.form = {
                                    ...data,
                                };
                        }
                    });
                },


                /** 提交按钮 */
                submitForm: function () {
                    this.$refs["form"].validate((valid) => {
                        if (valid) {
                            if (this.form.companyId != undefined && this.opertype === 2) {
                                modifyCompany(this.form)
                                    .then((res) => {
                                        this.msgSuccess("修改成功");
                                        this.open = false;
                                        this.getList();
                                        this.getCompanyTreeSelectList();
                                    })
                            }
                            else {
                                addCompany(this.form)
                                    .then((res) => {
                                    this.msgSuccess("新增成功");
                                    this.open = false;
                                    this.getList();
                                    this.getCompanyTreeSelectList();
                                })
                            }
                        }
                    });
                },

    },
};